package nus.ss.thread.ticket;

public class SaleWindow extends Thread {

    //初始化一个对象 表明卖的是什么
    Ticket t;

    //让窗口有一个名字,在构造方法里面加一个String name 初始化name
    public SaleWindow(Ticket t, String name) {
        super(name);
        this.t = t;
    }

    @Override
    public void run() {
        while (true) {

            //使得各个卖票窗口均衡往出卖票
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            //实现同步代码块 上锁

            synchronized (t) {
                if (t.getNum() > 0) {
                    System.out.println(Thread.currentThread().getName() + "正在卖出" + t.getNum() + "号票");
                    t.sale(); // num--
                } else {
                    System.out.println("很抱歉，票已卖完");
                    break;
                }
            }
        }
    }
}
